﻿/*
创建人：longb
日期：2022-06-01
描述：发送配送提请js
*/
var page = {
    workflowstepid: getParam("id"), //工序ID
    ContainerNo: getParam("ContainerNo"), //任务单号
    ContainerID: getParam("ContainerID"), //任务单ID //add by longb 2023-01-16
    mfgorderid: getParam("mfgorderid"),
    rootPath: getRootPath(), //网站根路径
    ajaxUrl: getRootPath() + "/Ashx/ZsHandler/ZSSendRequestHandler.ashx",
    //初始化页面
    init: function () {
        page.bind();
        page.GetBigSmallData();
    },
    //页面事件绑定
    bind: function () {
        //大件信息
        $('#BigSmallTable').datagrid({
            columns: [[
                    { field: 'PK', checkbox: true, width: 120, align: 'center' },
                    { field: 'WORKFLOWSTEPNAME', title: '工序', width: 100 },
		            { field: 'PRODUCTNAME', title: '件号', width: 100 },
		            { field: 'PRODUCTDESC', title: '件号名称', width: 150 },
                    { field: 'PARTTYPE', title: '类型', width: 100 },
                    { field: 'PRODUCTBATCHNO', title: '批次', width: 80 },
		            { field: 'FIRSTASSEMBLEQTY', title: '集件数量', width: 90, align: 'center' },
                    { field: 'TOTALCOLLECTQTY', title: '去封/集件报工数', width: 150, align: 'center' },
                    { field: 'STATUS', title: '是否发送', width: 150, align: 'center' }
                ]]
        });
        //辅材信息
        $('#AuxmaterialTable').datagrid({
            columns: [[
                    { field: 'PK', checkbox: true, width: 120, align: 'center' },
                    { field: 'WORKFLOWSTEPNAME', title: '工序', width: 100 },
		            { field: 'AUXMATERIALNAME', title: '辅材编码', width: 100 },
		            { field: 'DESCRIPTION', title: '辅材名称', width: 150 },
                    { field: 'SPECIFICATION', title: '规格', width: 150 },
                    { field: 'RESOURCETYPENAME', title: '类型', width: 100 },
		            { field: 'QTY', title: '需求数量', width: 90, align: 'center' },
                    { field: 'STATUS', title: '是否发送', width: 150, align: 'center' }
                ]]
        });
        $('#ToolsTable').datagrid({
            columns: [[
                    { field: 'PK', checkbox: true, width: 120, align: 'center' },
                    { field: 'WORKFLOWSTEPNAME', title: '工序', width: 100 },
		            { field: 'NAME', title: '工具编号', width: 100 },
		            { field: 'DESCRIPTION', title: '工具名称', width: 150 },
                    { field: 'SPECIFICATION', title: '规格', width: 150 },
                    { field: 'RESOURCETYPENAME', title: '类型', width: 100 },
		            { field: 'QTY', title: '需求数量', width: 90, align: 'center' },
                    { field: 'STATUS', title: '是否发送', width: 150, align: 'center' }
                ]]
        });
    },
    //大小件信息
    GetBigSmallData: function () {
        var param = {
            action: "GetBigSmallData",
            id: page.workflowstepid,
            ContainerNo: page.ContainerNo,
            ContainerID: page.ContainerID,//add by longb 2023-01-16
            mfgorderid:page.mfgorderid
        };
        page.post(param, function (res) {
            $('#BigSmallTable').datagrid('loadData', res.DBTable);
            $('#AuxmaterialTable').datagrid('loadData', res.ReturnData);
            $('#ToolsTable').datagrid('loadData', res.DBDataSet.Table1);
        });
    },
    //发起提请
    SendRequset: function (i) {
        var data = [];
        if (i == 1) {
            var rows = $('#BigSmallTable').datagrid('getChecked');
            $.each(rows, function (index, item) {
                if (item.STATUS == '未发送') {
                    item.ApplyType = "1";
                    //item.WORKFLOWSTEPID = page.workflowstepid;
                    item.PURPOSE = "";
                    data.push(item);
                }
            });
        }
        var Aux = [];
        if (i == 2) {
            var Auxrows = $('#AuxmaterialTable').datagrid('getChecked');
            $.each(Auxrows, function (index, item) {
                if (item.STATUS == '未发送') {
                    Aux.push(item);
                }
            });
        }
        var Tools = [];
        if (i == 3) {
            var Toolsrows = $('#ToolsTable').datagrid('getChecked');
            $.each(Toolsrows, function (index, item) {
                if (item.STATUS == '未发送') {
                    Tools.push(item);
                }
            });
        }
        if ((data.length + Aux.length + Tools.length) == 0) {
            displayMessage("工序所需" + (i==1?"大小件":(i==2?"辅材":"工装工具")) + "已全部发送提请");
            return;
        }
        var param = {
            action: "SendRequset",
            ApplyType: "1",
            ContainerNo: page.ContainerNo,
            data: JSON.stringify(data),
            Aux: JSON.stringify(Aux),
            Tools: JSON.stringify(Tools)
        };
        page.post(param, function (res) {
            if (res.Result) {
                page.GetBigSmallData();
            }
            displayMessage(res.Message, res.Result);
        });
    },
    //清除方法，重要！！！
    reset: function (iscondition, isresult) {
        clearMessage();
    },
    //post数据
    post: function (param, callback) {
        $.ajax({
            type: "Post",
            url: page.ajaxUrl,
            data: param,
            cashe: false,
            async: true,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
            }
        });
    }
}
$(function () {
    page.init();
});